using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OnChargeWave : OnChargeExplosionAdhesive
{
	[SerializeField]
	private float colliderGrowFactor = 5f;

	[SerializeField]
	private float particleGrowFactor = 0.5f;

	[SerializeField]
	private List<AudioClip> dubstepSamples = new List<AudioClip>();

	[SerializeField]
	private int numberOfSong = -1;

	[SerializeField]
	private GameObject particleToScale;

	[SerializeField]
	private GameObject meshForWeakDevice;

	private bool weakDevice;

	[SerializeField]
	private Collider damageTrigerOnCharge;

	[SerializeField]
	private AudioSource audioSourceOnCharge;

	[SerializeField]
	private WaveTriggerController waveDestroyTrigger;

	private List<Mortal> passedMortals = new List<Mortal>();

	public override void Awake()
	{
		base.Awake();
		base.gameObject.name = base.gameObject.name.Replace("(Clone)", string.Empty);
		if (audioSourceOnCharge == null)
		{
			audioSourceOnCharge = base.gameObject.GetComponent<AudioSource>();
		}
		if (damageTrigerOnCharge == null)
		{
			damageTrigerOnCharge = GetComponent<Collider>();
		}
		if (Device.indexWeakDevice == 2)
		{
			weakDevice = true;
			particleToScale.SetActive(false);
			meshForWeakDevice.SetActive(true);
		}
	}

	private new void Start()
	{
		if (numberOfSong == -1)
		{
			audioSourceOnCharge.clip = dubstepSamples[Random.Range(0, dubstepSamples.Count)];
		}
		else
		{
			audioSourceOnCharge.clip = dubstepSamples[numberOfSong];
		}
		audioSourceOnCharge.Play();
	}

	private IEnumerator IncreasingWave(float maxDistance)
	{
		while (traveledDistance < maxDistance && damageTrigerOnCharge.enabled)
		{
			base.gameObject.transform.localScale += new Vector3(1f, 1f, 0f) * Time.deltaTime * colliderGrowFactor;
			if (!weakDevice)
			{
				particleToScale.transform.localScale += new Vector3(1f, 0f, 1f) * Time.deltaTime * particleGrowFactor;
			}
			yield return new WaitForEndOfFrame();
		}
		while (audioSourceOnCharge.isPlaying)
		{
			yield return new WaitForEndOfFrame();
		}
		if (base.isMine)
		{
			Destroy();
		}
	}

	public override IEnumerator CalculateTravelDistance(float maxDistance)
	{
		return base.CalculateTravelDistance(maxDistance);
	}

	public void IncreaseWave(float maxDistance)
	{
		if (settings.offlineMode)
		{
			SyncScale(maxDistance);
			return;
		}
		base.photonView.RPC("SyncScale", PhotonTargets.All, maxDistance);
	}

	[PunRPC]
	public void SyncScale(float maxDistance)
	{
		StartCoroutine(CalculateTravelDistance(maxDistance));
		StartCoroutine(IncreasingWave(maxDistance));
	}

	public override void OnTriggerEnter(Collider other)
	{
		if (base.isMine && ignoreCollider != null && other == ignoreCollider)
		{
			return;
		}
		if (base.isMine && ignoreColliders != null)
		{
			for (int i = 0; i < ignoreColliders.Length; i++)
			{
				if (other == ignoreColliders[i])
				{
					return;
				}
			}
		}
		if (!base.isMine)
		{
			return;
		}
		Mortal componentInParent = other.gameObject.GetComponentInParent<Mortal>();
		if (passedMortals.Contains(componentInParent))
		{
			return;
		}
		if ((bool)componentInParent && TDMController.isTeamGameType && base.photonView.isMine)
		{
			if (componentInParent is EntityBehavior)
			{
				EntityBehavior entityBehavior = componentInParent as EntityBehavior;
				PlayerBehavior playerBehavior = entityBehavior.getMainHuman() as PlayerBehavior;
				PlayerBehavior playerBehavior2 = GameController.instance.FindPlayer(entityBehavior.photonView.ownerId);
				if ((entityBehavior.getMainHuman() != null && playerBehavior != null && playerBehavior.localTeam == TDMController.myTeam && !entityBehavior.isMine) || (entityBehavior.privateCar && playerBehavior2 != null && playerBehavior2.localTeam == TDMController.myTeam))
				{
					return;
				}
			}
			else if (componentInParent is PlayerBehavior)
			{
				PlayerBehavior playerBehavior3 = componentInParent as PlayerBehavior;
				if (playerBehavior3.localTeam == TDMController.myTeam && !playerBehavior3.isMine)
				{
					return;
				}
			}
		}
		if (componentInParent is Humanoid)
		{
			passedMortals.Add(componentInParent);
			SendDamage(componentInParent, damage, Vector3.down * 100f);
		}
		else if (componentInParent is EntityBehavior)
		{
			passedMortals.Add(componentInParent);
			SendDamage(componentInParent, damage, Vector3.down * 100f);
		}
		else if (componentInParent is CRGate)
		{
			passedMortals.Add(componentInParent);
			SendDamage(componentInParent, damage, Vector3.down * 100f);
		}
	}

	public override void BlowUp()
	{
	}

	public override void OnDisable()
	{
		if (settings.offlineMode)
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void Destroy()
	{
		if ((bool)base.gameObject)
		{
			if (settings.offlineMode)
			{
				Object.Destroy(base.gameObject);
			}
			else
			{
				PhotonNetwork.Destroy(base.gameObject);
			}
		}
	}

	[PunRPC]
	public void Hide()
	{
		waveDestroyTrigger.Hide();
		if (damageTrigerOnCharge != null)
		{
			damageTrigerOnCharge.enabled = false;
		}
		if (!weakDevice && particleToScale != null)
		{
			particleToScale.SetActive(false);
		}
		if (weakDevice && meshForWeakDevice != null)
		{
			meshForWeakDevice.SetActive(false);
		}
	}

	protected override void OnDestroy()
	{
	}
}
